package com.tanglinkai.mall.filter;

import lombok.extern.slf4j.Slf4j;import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;

@Slf4j
public class AuthFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        log.debug("req.getRequestURI():{}", req.getRequestURI());
        if (req.getRequestURI().endsWith("/admin/login")) {
            chain.doFilter(request, response); //放行
            return;
        }

        HttpSession session = req.getSession();
        if (session.getAttribute("manager") == null) {
            resp.sendRedirect("/admin/login");
            return;
        }
        chain.doFilter(request, response); //放行
    }
}